/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import java.awt.Graphics2D;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of filling-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Fill {

    /**
     * No instance of this class.
     */
    private Fill() {
    } // end empty constructor

    /**
     * Fills a rectangle.
     * @param ctxt context
     * @param vx x-component of lower left corner coordinate
     * @param vy y-component of lower left corner coordinate
     * @param vw width
     * @param vh height
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_fill_rect(final CodeRunner ctxt,
                                          final Value vx,
                                          final Value vy,
                                          final Value vw,
                                          final Value vh)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int w = vw.asLong();
        final int h = vh.asLong();
        final int x = vx.asLong();
        final int y = slot.convertY(vy.asLong()) - h;
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.fillRect(x, y, w, h);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.fillRect(x, y, w, h);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_fill_rect(CodeRunner, Value, Value, ...)'

    /**
     * Fills a polygon.
     * @param ctxt context
     * @param array array of <i>(x, y)</i> coordinates of polygon points
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_fill_poly(final CodeRunner ctxt,
                                          final Value array)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final Block pts = array.asBlock();
        final int len = pts.getWoSize();
        final int[] x = new int[len];
        final int[] y = new int[len];
        for (int i = 0; i < len; i++) {
            final Block pt = pts.get(i).asBlock();
            x[i] = pt.get(0).asLong();
            y[i] = slot.convertY(pt.get(1).asLong());
        } // end for
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.fillPolygon(x, y, len);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.fillPolygon(x, y, len);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_fill_poly(CodeRunner, Value)'

    /**
     * Fills an elliptical arc.
     * @param ctxt context
     * @param vx x-component of center
     * @param vy y-component of center
     * @param vrx horizontal radius
     * @param vry vertical radius
     * @param va1 starting angle
     * @param va2 ending angle
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_fill_arc(final CodeRunner ctxt,
                                         final Value vx,
                                         final Value vy,
                                         final Value vrx,
                                         final Value vry,
                                         final Value va1,
                                         final Value va2)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int rx = vrx.asLong();
        final int ry = vry.asLong();
        final int x = vx.asLong() - rx;
        final int y = slot.convertY(vy.asLong()) - ry;
        final int w = rx * 2;
        final int h = ry * 2;
        final int sa = va1.asLong();
        final int aa = va2.asLong() - sa;
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.fillArc(x, y, w, h, sa, aa);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.fillArc(x, y, w, h, sa, aa);
            store.dispose();
        } // end if
        return Value.UNIT;
    } // end method 'caml_gr_fill_arc(CodeRunner, Value, Value, ...)'

    /**
     * Exact synonym of {@link #caml_gr_fill_arc(CodeRunner, Value, Value, Value, Value, Value, Value)}.
     */
    @Primitive
    public static Value caml_gr_fill_arc_nat(final CodeRunner ctxt,
                                             final Value vx,
                                             final Value vy,
                                             final Value vrx,
                                             final Value vry,
                                             final Value va1,
                                             final Value va2)
        throws Fail.Exception {
        return caml_gr_fill_arc(ctxt, vx, vy, vrx, vry, va1, va2);
    } // end method 'caml_gr_fill_arc_nat(CodeRunner, Value, Value, Value, Value, Value, Value)'

} // end class 'Fill'
